<template>
  <!-- 员工实名认证审核 -->
  <div>
    <!-- 第一行 -->
    <!-- {{ applys }} -->
    <div class="header">
      <el-form :inline="true" :model="params" class="demo-form-inline">
        <!-- 时间 -->
        <!-- {{ timeToTime }} -->
        <el-date-picker
          v-model="timeToTime"
          value-format="timestamp"
          type="datetimerange"
          :picker-options="pickerOptions"
          range-separator="至"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          align="right"
          clearable
        />
        <!-- 请选择状态 -->
        <el-form-item>
          <el-select v-model="params.status" placeholder="请选择状态" clearable>
            <el-option label="通过" value="通过" />
            <el-option label="不通过" value="不通过" />
            <el-option label="未审核" value="未审核" />
          </el-select>
        </el-form-item>
        <!-- 申请人编号 -->
        <el-form-item>
          <el-input
            v-model="params.userId"
            placeholder="请输入申请人编号"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSearch">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 第一行结束 -->

    <!-- 第二行 -->
    <!-- {{ params }} -->
    <el-table v-loading="loading" :data="applyData" style="width: 100%">
      <el-table-column type="index" :index="1" label="序号" />
      <el-table-column prop="id" label="申请人编号" width="100px" />
      <el-table-column prop="realname" label="姓名" />
      <el-table-column prop="applyTime" label="申请时间">
        <!-- 用插槽绑定当前行的时间戳才可转换格式 -->
        <template slot-scope="scope">
          {{ scope.row.applyTime | fmtDate }}
        </template>
      </el-table-column>
      <el-table-column prop="idCard" label="身份证号" width="180px" />
      <el-table-column prop="idcardPhotoPositive" label="身份证正面">
        <template slot-scope="scope">
          <!-- el-popover 弹出框 trigger='hover' 悬浮触发  placement="right" 设置弹出位置在右侧 -->
          <el-popover placement="right" trigger="hover">
            <img
              slot="reference"
              :src="scope.row.photo"
              width="30px"
              alt="加载失败"
            >
            <img :src="scope.row.photo" width="200px" alt="加载失败">
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column prop="idcardPhotoNegative" label="身份证反面">
        <template slot-scope="scope">
          <!-- el-popover 弹出框 trigger='hover' 悬浮触发  placement="right" 设置弹出位置在右侧 -->
          <el-popover placement="right" trigger="hover">
            <img
              slot="reference"
              :src="scope.row.photo"
              width="30px"
              alt="加载失败"
            >
            <img :src="scope.row.photo" width="200px" alt="加载失败">
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column prop="bankCard" label="银行卡号" width="180px" />
      <el-table-column prop="bankCardPhoto" label="银行卡">
        <template slot-scope="scope">
          <!-- el-popover 弹出框 trigger='hover' 悬浮触发  placement="right" 设置弹出位置在右侧 -->
          <el-popover placement="right" trigger="hover">
            <img
              slot="reference"
              :src="scope.row.photo"
              width="30px"
              alt="加载失败"
            >
            <img :src="scope.row.photo" width="200px" alt="加载失败">
          </el-popover>
        </template>
      </el-table-column>
      <!-- 状态 -->
      <el-table-column prop="status" label="状态">
        <template slot-scope="scope">
          <el-tag
            v-if="scope.row.status === '通过'"
            type="success"
          >通过</el-tag>
          <el-tag
            v-else-if="scope.row.status === '不通过'"
            type="danger"
          >不通过</el-tag>
          <el-tag v-else type="info">未审核</el-tag>
        </template>
      </el-table-column>
      <!-- 操作 -->
      <el-table-column label="操作">
        <template v-if="scope.row.status === '未审核'" slot-scope="scope">
          <!-- scope.row 将当前行的数据传给这个方法 -->
          <a
            href="#"
            style="color: #30b08f"
            @click.prevent="changeStatus(scope.row.id, '通过')"
          >通过</a>
          <a
            href="#"
            style="color: #e65d6e"
            @click.prevent="changeStatus(scope.row.id, '不通过')"
          >不通过</a>
        </template>
      </el-table-column>
    </el-table>
    <!-- 第二行结束 -->

    <!-- 第三行 分页按钮 -->
    <div class="page">
      <!-- :current-page="params.page" 默认当前页
          :page-size="params.pageSize" 默认一页多少条
      -->
      <el-pagination
        :current-page="params.page"
        :page-sizes="[2, 5, 10, 20]"
        :page-size="params.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <!-- 第三行结束 -->
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { queryApply, pass, noPass } from '@/api/certification'
export default {
  data() {
    return {
      params: {
        page: 1,
        pageSize: 10,
        beginTime: '',
        endTime: ''
      },
      timeToTime: [],
      // 控制白屏的加载动画
      loading: false,
      // 实名认证申请的数据
      applyData: [],
      // 申请总条数
      total: 0,
      // 第一行时间组件
      pickerOptions: {
        shortcuts: [
          {
            text: '最近一周',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
              picker.$emit('pick', [start, end])
            }
          }
        ]
      }
    }
  },
  computed: {
    ...mapGetters(['applys'])
  },
  created() {
    // 查询所有申请 --分发申请动作
    this.$store.dispatch('apply/queryApplys', { page: 1, pageSize: 1000 })
    this.getApply()
  },
  mounted() {},
  methods: {
    // 查询申请事件
    onSearch() {
      this.params.page = 1
      this.getApply()
    },

    // 分页查询实名认证申请
    async getApply() {
      // 优化：数据获取之前加载动画
      this.loading = true
      // 时间
      if (this.timeToTime) {
        this.params.beginTime = this.timeToTime[0]
        this.params.endTime = this.timeToTime[1]
      } else {
        delete this.params.beginTime
        delete this.params.endTime
      }
      for (const key in this.params) {
        // key --属性名
        // this.params[key] --属性值
        if (!this.params[key]) {
          // 如果这个属性值不存在（为空） 就把这个属性删掉
          delete this.params[key]
        }
      }
      const res = await queryApply(this.params)
      // console.log(res, "获取响应");
      this.applyData = res.data.list
      this.total = res.data.total

      // 关闭加载动画
      this.loading = false
    },
    // 页码条数发生改变的方法 默认参数pageSize
    handleSizeChange(pageSize) {
      this.params.pageSize = pageSize
      this.params.page = 1 // 页码条数改变时 刷新当前页为第一页
      this.getApply() // 修改完后重新获取数据
    },
    // 当前页码发生改变触发的方法 默认参数page
    handleCurrentChange(page) {
      this.params.page = page
      this.getApply() // 修改完后重新获取数据
    },

    // 操作通过或者未通过
    changeStatus(id, status) {
      if (status === '通过') {
        // 提示框
        this.$confirm('执行通过，是否确定？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(async() => {
            const res = await pass({ id })
            this.$message({
              type: 'success',
              message: res.massage
            })
            // 刷新
            this.getApply()
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消操作'
            })
          })
      } else {
        // 提示框
        this.$confirm('执行不通过，是否确定？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(async() => {
            const res = await noPass({ id })
            this.$message({
              type: 'success',
              message: res.massage
            })
            // 刷新
            this.getApply()
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消操作'
            })
          })
      }
    }
  }
}
</script>
<style scoped>
</style>
